import 'package:flutter/material.dart';
import 'package:maihealth/class/file/filePage.dart';
import 'package:maihealth/class/guan/guanPage.dart';
import 'package:maihealth/class/health/healthPage.dart';
import 'package:maihealth/class/home/page/homePage.dart';
import 'package:maihealth/class/profile/page/profilePage.dart';



class mainPage extends StatefulWidget {
  @override
  _mainPageState createState() => _mainPageState();
}

class _mainPageState extends State<mainPage> {
int _tabIndex = 0;
  var tabImages;
  var appBarTitles = ['首页', '医馆', '脉健康','档案','我的'];
  /*
   * 根据选择获得对应的normal或是press的icon
   */
  Image getTabIcon(int curIndex) {
    if (curIndex == _tabIndex) {
      return tabImages[curIndex][1];
    }
    return tabImages[curIndex][0];
  }
  /*
   * 获取bottomTab的颜色和文字
   */
  Text getTabTitle(int curIndex) {
    if (curIndex == _tabIndex) {
      return new Text(appBarTitles[curIndex],
          style: new TextStyle(fontSize: 14.0, color: const Color(0xffe67c3f)));
    } else {
      return new Text(appBarTitles[curIndex],
          style: new TextStyle(fontSize: 14.0, color: const Color(0xff515151)));
    }
  }
  /*
   * 根据image路径获取图片
   */
  Image getTabImage(path) {
    return new Image.asset(path, width: 24.0, height: 24.0);
  }
 
 
  void initData() {
    /*
     * 初始化选中和未选中的icon
     */
    tabImages = [
      [getTabImage('images/main/tab_home.png'), getTabImage('images/main/tab_home_pre.png')],
      [getTabImage('images/main/tab_yg.png'), getTabImage('images/main/tab_yg_pre.png')],
      [getTabImage('images/main/tab_home.png'), getTabImage('images/main/tab_home_pre.png')],
      [getTabImage('images/main/tab_da.png'), getTabImage('images/main/tab_da_pre.png')],
      [getTabImage('images/main/tab_my.png'), getTabImage('images/main/tab_my_pre.png')],
    ];

  }
 //初始化Tab页面
 final List<Widget> _pageList = [
      homePage(),
      guanPage(),
      healthPage(),
      filePage(),
      profilePage()
  ];
  @override
  Widget build(BuildContext context) {
    //初始化数据
    initData();
    return Scaffold(
        body: IndexedStack(
            index: _tabIndex,
            children: _pageList
         ),
        bottomNavigationBar: new BottomNavigationBar(
          items: <BottomNavigationBarItem>[
            new BottomNavigationBarItem(
                icon: getTabIcon(0), title: getTabTitle(0)),
            new BottomNavigationBarItem(
                icon: getTabIcon(1), title: getTabTitle(1)),
            new BottomNavigationBarItem(
                icon: getTabIcon(2), title: getTabTitle(2)),
            new BottomNavigationBarItem(
                icon: getTabIcon(3), title: getTabTitle(3)),
            new BottomNavigationBarItem(
                icon: getTabIcon(4), title: getTabTitle(4)),
          ],
          type: BottomNavigationBarType.fixed,
          //默认选中首页
          currentIndex: _tabIndex,
          iconSize: 24.0,
          //点击事件
          onTap: (index) {
            setState(() {
              _tabIndex = index;
            });
          },
        ));
  }
}
